package com.ease.core.components {
    import com.ease.core.components.ToolBar;
    import com.ease.core.events.DockingEvent;
    import com.ease.core.events.ToolBarEvent;
    import com.ease.core.managers.PerspectiveManager;
    import com.ease.core.managers.ViewManager;
    
    import flash.events.Event;
    import flash.events.FocusEvent;
    import flash.events.MouseEvent;
    
    import mx.binding.utils.BindingUtils;
    import mx.collections.ArrayCollection;
    import mx.containers.Box;
    import mx.containers.HBox;
    import mx.controls.Label;
    import mx.core.EdgeMetrics;
    import mx.core.ScrollPolicy;
    import mx.core.UIComponent;
    import mx.events.FlexEvent;
    import mx.managers.IFocusManagerComponent;

    public class DockingView extends Box implements IFocusManagerComponent {
        private static const CONTROL_BAR_HEIGHT:Number = 24;

        public var viewId:String;
        public var perspectiveId:String;
        public var index:int = 0;
        public var location:String = "bottom";

        protected var controlBar:HBox;
        protected var toolBar:ToolBar;
        protected var viewLabel:Label;

        [Bindable]
        private var _toolBarDataProvider:Object = null;
        [Bindable]
        private var _viewMenuDataProvider:Object = null;

        public function DockingView() {
            super();
            /** common */
            label = "untitled";
            styleName = "dockingView"
            addEventListener(DockingEvent.CLOSED, onViewClosed);
            addEventListener(FlexEvent.SHOW, onShowHide);
            addEventListener(FlexEvent.HIDE, onShowHide);
            BindingUtils.bindSetter(onAcitiveViewChange, ViewManager.getInstance(), "activeViewWindow");
        }
        
        protected function onShowHide(event:FlexEvent):void {
           
        }

        public function onAcitiveViewChange(value:String):void {
            actived = value == Object(parent).uid && Object(parent).selectedChild == this;
        }

        public var actived:Boolean = false;

        public function appendParam(spec:Object):void {

        }
        
        public function checkParam(spec:Object):Boolean {
           return false;
        }

        private function onLabelChanged(event:Event):void {
            if (viewLabel) {
                viewLabel.text = label;
            }
        }

        protected function createControlBar():void {
            if (null == controlBar) {
                controlBar = new HBox();
                controlBar.enabled = true;
                controlBar.setStyle("borderColor", 0x999999);
                controlBar.setStyle("borderThickness", 1);
                controlBar.setStyle("borderStyle", "solid");
                controlBar.setStyle("borderSides", "bottom");
                controlBar.setStyle("verticalAlign", "top");
                //  horizontalAlign="center"
                controlBar.setStyle("paddingTop", 0);
                controlBar.setStyle("paddingBottom", 0);
                controlBar.setStyle("backgroundColor", 0xEFEFEF);
                controlBar.setStyle("horizontalGap", 0);
                controlBar.setStyle("verticalGap", 0);
                //				labelCtrl.text="Problems";
                controlBar.percentWidth = 100;
                controlBar.height = CONTROL_BAR_HEIGHT; //toolbar height + bottom line(borderThickness)
            }
            createControlBarChildren();

//			addChild(controlBar);
        }

        override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void {
            super.updateDisplayList(unscaledWidth, unscaledHeight);
        }

        protected function createControlBarChildren():void {
            createViewLabel();
            createToolBar();
        }

        protected function createViewLabel():void {
            if (null == viewLabel) {
                viewLabel = new Label();
                //viewLabel.setStyle("backgroundColor", 0x0000FF);
                viewLabel.setStyle("paddingTop", 3);
                viewLabel.percentWidth = 100;
                viewLabel.percentHeight = 100;
                viewLabel.text = label;
                controlBar.addChild(viewLabel);
                addEventListener("labelChanged", onLabelChanged);
            }
        }

        protected function createToolBar():void {
            if (null == toolBar) {
                toolBar = new ToolBar();
                toolBar.addEventListener(ToolBarEvent.CLICK, onToolBarClick);
                //toolBar.addEventListener(MenuEvent.ITEM_CLICK, onItemClick);
                //toolBar.dataProvider=_toolBarDataProvider;
                BindingUtils.bindProperty(toolBar, "dataProvider", this, "toolBarDataProvider");
                controlBar.addChild(toolBar);
            }
        }


        override protected function createChildren():void {
            createControlBar();
            super.createChildren();
        }

        public function onViewMenuPopUpButton(event:MouseEvent):void {

        }

        public function onToolBarClick(event:ToolBarEvent):void {
        }

        public function onSizeChangeWatcher(event:Event):void {
            viewLabel.text = label + " (width: " + width + ",height: " + height + ")";
        }

        public function closable():Boolean {
            return true;
        }

        public function onViewClosed(event:DockingEvent):void {
            dispose();
        }

        public function dispose():void {
           ViewManager.getInstance().removeViewInstance(this, PerspectiveManager.getInstance().activePerspective);
        }

        public function isDockAccepted():Boolean {
            return true;
        }

        public function set toolBarDataProvider(value:Object):void {
            _toolBarDataProvider = value;
        }

        public function get toolBarDataProvider():Object {
            return _toolBarDataProvider;
        }
    }
}